<template>
  <div class="review-list">
    <!-- 评价统计区域 -->
    <div class="review-summary" v-if="averageRating > 0">
      <div class="rating-stats">
        <div class="average-score">{{ averageRating.toFixed(1) }}</div>
        <div class="star-display">
          <i v-for="i in 5" :key="i" 
             :class="['fa', i <= Math.round(averageRating) ? 'fa-star' : 'fa-star-o']"></i>
        </div>
        <div class="review-count">{{ reviews.length }}条评价</div>
      </div>
    </div>
    
    <!-- 空状态 -->
    <div class="empty-state" v-if="reviews.length === 0 && !loading">
      <i class="fa fa-comment-o empty-icon"></i>
      <p>暂无评价</p>
    </div>
    
    <!-- 加载状态 -->
    <div class="loading-state" v-if="loading">
      <div class="spinner"></div>
      <p>加载中...</p>
    </div>
    
    <!-- 评价列表 -->
    <div class="review-items" v-if="reviews.length > 0">
      <div class="review-item" v-for="review in reviews" :key="review.id">
        <!-- 用户信息 -->
        <div class="user-info">
          <div class="avatar">
            <i class="fa fa-user-circle-o"></i>
          </div>
          <div class="user-details">
            <div class="username">{{ review.userName }}</div>
            <div class="review-time">{{ review.reviewTime }}</div>
          </div>
        </div>
        
        <!-- 评分 -->
        <div class="review-rating">
          <i v-for="i in 5" :key="i" 
             :class="['fa', i <= review.rating ? 'fa-star' : 'fa-star-o']"></i>
        </div>
        
        <!-- 评价内容 -->
        <div class="review-content" v-if="review.content">
          {{ review.content }}
        </div>
        
        <!-- 商品信息 -->
        <div class="food-info" v-if="review.foodName">
          <span class="food-tag">评价的商品：</span>
          <span class="food-name">{{ review.foodName }}</span>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted, computed, defineProps } from 'vue';

const props = defineProps({
  businessId: {
    type: Number,
    default: null
  },
  foodId: {
    type: Number,
    default: null
  }
});

const reviews = ref([]);
const loading = ref(false);
const averageRating = ref(0);

// 计算平均评分
const calculateAverageRating = () => {
  if (reviews.value.length === 0) return 0;
  
  const sum = reviews.value.reduce((total, review) => total + review.rating, 0);
  return sum / reviews.value.length;
};

// 加载评价列表
const loadReviews = async () => {
  loading.value = true;
  
  try {
    // 导入API工具
    const { default: axios } = await import('axios');
    
    // 根据传入的props决定加载哪种评价
    let endpoint = '';
    if (props.businessId) {
      endpoint = `/api/reviews/business/${props.businessId}`;
    } else if (props.foodId) {
      endpoint = `/api/reviews/food/${props.foodId}`;
    } else {
      throw new Error('必须提供businessId或foodId');
    }
    
    // 发送请求
    const response = await axios.get(endpoint);
    
    // 处理响应
    if (response.data && response.data.success) {
      reviews.value = response.data.data || [];
    } else {
      reviews.value = [];
    }
    
    // 计算平均评分
    averageRating.value = calculateAverageRating();
    
    // 如果是商家评价，获取服务端计算的平均评分
    if (props.businessId) {
      const ratingResponse = await axios.get(`/api/reviews/business/${props.businessId}/rating`);
      if (ratingResponse.data && ratingResponse.data.success) {
        averageRating.value = ratingResponse.data.data;
      }
    }
    
  } catch (error) {
    console.error('加载评价失败:', error);
    reviews.value = [];
  } finally {
    loading.value = false;
  }
};

// 组件挂载时加载评价
onMounted(() => {
  loadReviews();
});
</script>

<style scoped>
.review-list {
  background-color: #fff;
  border-radius: 8px;
  padding: 15px;
  margin-bottom: 15px;
}

.review-summary {
  margin-bottom: 20px;
  padding-bottom: 15px;
  border-bottom: 1px solid #f0f0f0;
}

.rating-stats {
  display: flex;
  align-items: center;
  justify-content: center;
  flex-direction: column;
}

.average-score {
  font-size: 36px;
  font-weight: bold;
  color: #ff6b35;
  margin-bottom: 5px;
}

.star-display {
  margin-bottom: 5px;
}

.star-display i {
  color: #ffc107;
  font-size: 18px;
  margin: 0 2px;
}

.review-count {
  color: #999;
  font-size: 14px;
}

.empty-state, .loading-state {
  text-align: center;
  padding: 30px 0;
  color: #999;
}

.empty-icon {
  font-size: 48px;
  margin-bottom: 10px;
  color: #ddd;
}

.spinner {
  width: 30px;
  height: 30px;
  margin: 0 auto 10px;
  border: 3px solid rgba(0, 151, 255, 0.2);
  border-top-color: #0097ff;
  border-radius: 50%;
  animation: spin 1s infinite linear;
}

@keyframes spin {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}

.review-items {
  padding-top: 10px;
}

.review-item {
  padding: 15px 0;
  border-bottom: 1px solid #f0f0f0;
}

.review-item:last-child {
  border-bottom: none;
}

.user-info {
  display: flex;
  align-items: center;
  margin-bottom: 10px;
}

.avatar {
  width: 40px;
  height: 40px;
  margin-right: 10px;
  border-radius: 50%;
  overflow: hidden;
  background-color: #f5f5f5;
  display: flex;
  align-items: center;
  justify-content: center;
}

.avatar i {
  font-size: 24px;
  color: #ccc;
}

.user-details {
  flex: 1;
}

.username {
  font-size: 14px;
  font-weight: 500;
  color: #333;
  margin-bottom: 2px;
}

.review-time {
  font-size: 12px;
  color: #999;
}

.review-rating {
  margin-bottom: 10px;
}

.review-rating i {
  color: #ffc107;
  font-size: 16px;
  margin-right: 3px;
}

.review-content {
  font-size: 14px;
  line-height: 1.5;
  color: #333;
  margin-bottom: 10px;
  white-space: pre-wrap;
  word-break: break-word;
}

.food-info {
  padding: 8px;
  background-color: #f9f9f9;
  border-radius: 4px;
  font-size: 12px;
}

.food-tag {
  color: #999;
}

.food-name {
  color: #333;
}
</style> 